package com.xrz.sxm.aj.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.xrz.sxm.aj.activity.R;
import com.xrz.sxm.aj.view.PieProgress;

public class XUtilBitmapUtil {

	private static XUtilBitmapUtil xb;
	private BitmapUtils bu;
	private Context c;

	public static XUtilBitmapUtil getInstant() {
		if (xb == null)
			xb = new XUtilBitmapUtil();
		return xb;
	}

	private void init() {
		if (bu == null) {
			bu = new BitmapUtils(c);
			bu.configDefaultLoadFailedImage(R.mipmap.icon_default_img);
			bu.configDefaultLoadingImage(R.mipmap.icon_img_loading);
		}
	}

	public void display(Context c, View v, String url) {
		this.c = c;
		init();
		bu.display(v, url);   
	}
	
	public void displayByWidth(Context c, ImageView imageView,String url){
		this.c = c;
		init();
		bu.display(imageView, url, new LoadCallBack1());  
	}

	private class LoadCallBack1 extends BitmapLoadCallBack<ImageView>{

		@Override
		public void onLoadCompleted(ImageView arg0, String arg1, Bitmap arg2,
				BitmapDisplayConfig arg3, BitmapLoadFrom arg4) {
			LayoutParams params = (LayoutParams) arg0.getLayoutParams();
			int oldWidth = arg2.getWidth();
			int oldHight = arg2.getHeight();
			int newWidth = ScreenUtil.getInstant().getDisWidth(c);
			int newHight = (oldHight * newWidth) / oldWidth;
			Log.e("----", newHight+"");
			params.width = ScreenUtil.getInstant().getDisWidth(c);
			params.height = newHight;
			arg0.setLayoutParams(params);
			arg0.setImageBitmap(arg2);
		}

		@Override
		public void onLoadFailed(ImageView arg0, String arg1, Drawable arg2) {
			// TODO Auto-generated method stub
			
		}
		
	}
	/**
	 * 
	 * ����ͼƬ
	 */
	public void display(Context c, final View view,PieProgress pbLoading, String url) {
		this.c = c;
		init();
		bu.display(view, url, new LoadCallBack(view,pbLoading));  
	}
	
	private class LoadCallBack extends BitmapLoadCallBack<View>{
		PieProgress progressBar;
		View view;
		LoadCallBack(View view,PieProgress progressBar){
			this.progressBar = progressBar;
			this.view = view;
		}
		@Override
		public void onLoadCompleted(View arg0, String arg1, Bitmap arg2,
				BitmapDisplayConfig arg3, BitmapLoadFrom arg4) {
			if(view instanceof ImageView){
				((ImageView) view).setScaleType(ScaleType.CENTER_CROP);
				((ImageView) view).setImageBitmap(arg2);
			}
			progressBar.setVisibility(View.GONE);
		}

		@Override
		public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
			
		}
		
		@Override
		public void onLoading(View container, String uri,
				BitmapDisplayConfig config, long total, long current) {
			super.onLoading(container, uri, config, total, current);
			float progress = (float)current / (float)total;
			Log.e("---", "progress="+progress);
			progressBar.setProgress((int) (progress*360));
		}
		
	}

}
